package learn_project;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

/**
 * @author Vv
 * @version 1.0
 * @project coding_learn
 * @description
 * @date 2023/9/25 08:59:39
 */
public class BusinessMng {
    private int businessId;
    private String password;
    private String businessName;
    private String buinessAddress;
    private String businessExplain;
    private double starPrices;
    private double deliveryPrice;

    public int getBusinessId() {
        return businessId;
    }

    public void setBusinessId(int businessId) {
        this.businessId = businessId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getBusinessName() {
        return businessName;
    }

    public void setBusinessName(String businessName) {
        this.businessName = businessName;
    }

    public String getBuinessAddress() {
        return buinessAddress;
    }

    public void setBuinessAddress(String buinessAddress) {
        this.buinessAddress = buinessAddress;
    }

    public String getBusinessExplain() {
        return businessExplain;
    }

    public void setBusinessExplain(String businessExplain) {
        this.businessExplain = businessExplain;
    }

    public double getStarPrices() {
        return starPrices;
    }

    public void setStarPrices(double starPrices) {
        this.starPrices = starPrices;
    }

    public double getDeliveryPrice() {
        return deliveryPrice;
    }

    public void setDeliveryPrice(double deliveryPrice) {
        this.deliveryPrice = deliveryPrice;
    }
    public void queryBusiness(){
        System.out.println("显示当前登录的商户信息");
        System.out.println("按回车键后回到menu02(商户管理2级菜单)");
        Scanner scanner = new Scanner(System.in);
        //暂停程序
        System.out.println(this.toString());
        String str = scanner.nextLine();
        Menu.menu02();
    }
    public void modifyBusiness() throws IOException {
        System.out.println("修改当前登录的商户信息");
        System.out.println("按回车键后回到menu02(商户管理2级菜单)");
        Scanner scanner = new Scanner(System.in);
        //this.toString();
        String explain = scanner.nextLine();
        setBusinessExplain(explain);
        List<BusinessMng> businesses = fileToList();
        Boolean flag = false;
        for(int i = 0;i<businesses.size();i++){
            if(businesses.get(i).equals(this)){
                businesses.set(i,this);
                flag = true;
            }
        }
        if(!flag){
            businesses.add(this);
        }
        ListToFile(businesses);
        String str = scanner.nextLine();
        Menu.menu02();
    }
    public void deleteBusiness() throws IOException {
        System.out.println("删除当前登录的商户信息");
        System.out.println("按回车键后回到menu02(商户管理2级菜单)");
        Scanner scanner = new Scanner(System.in);
        List<BusinessMng> list = fileToList();
        int N = list.size();
        int index = -1;
        for(int i = 0;i<N;i++){
            //boolean f = this.equals(list.get(i));
            if(this.equals(list.get(i))){
                index = i;
            }
        }
        if(index != -1){
            list.remove(index);
        }
        ListToFile(list);
        String str = scanner.nextLine();
        Menu.menu02();
    }
    // 商户食品管理
    public void businessFood() {
        System.out.println("商户食品管理菜单出现...");
        System.out.println();
        Menu.menu02();
    }

    // 商户注册
    // private int businessId;
    //    private String password;
    //    private String businessName;
    //    private String buinessAddress;
    //    private String businessExplain;
    //    private double starPrices;
    //    private double deliveryPrice;
    public static void businessReg() {
        System.out.println("通过逐步提示 让商户利用键盘输入 注册信息");
        System.out.println("注册成ntln(按回车键后回到menu02(商户管理 2级菜单)");
        Scanner sc = new Scanner(System.in);
        // 暂停程序
        //从这里开始让商户逐渐创建一个
        System.out.println("请问你现在是准备开始创建商户账号么？ Y / N");
        if(sc.next() == "N"){
            return ;
        }
        System.out.println("请输入你商户的名字 请确保其中不要出现空格");

        String strName = sc.next();

        // 密码生成
        {
            String PassW1 = null;
            String PassW2 = null;
            do{
                System.out.println("请输入你要申请的账号的密码 ： ");
                PassW1 = sc.next();
                System.out.println("请在此输入你的密码");
                PassW2 = sc.next();
                if(!PassW1.equals(PassW2)){
                    System.out.println("密码两次输入不正确 请重新输入");
                }
            }while(!PassW1.equals(PassW2));
        }



        System.out.println("按回车键...");
        // 暂停程序
        String str = sc.nextLine();
        Menu.menu01();
    }

    // 商户登录
    public static void businessLogin() {
        System.out.println("通过逐步提示 让商户利用键盘输入 登录信息");
        System.out.println("登录成功后进入menu02(商户管理 2级菜单)");
        //System.out.println("登录失败后返回menu01(1级菜单)");
        System.out.println("按回车键...");
        Scanner sc = new Scanner(System.in);

        String str = sc.nextLine();
        Menu.menu02();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BusinessMng that = (BusinessMng) o;
        return businessId == that.businessId && Double.compare(starPrices, that.starPrices) == 0 && Double.compare(deliveryPrice, that.deliveryPrice) == 0 && Objects.equals(password, that.password) && Objects.equals(businessName, that.businessName) && Objects.equals(buinessAddress, that.buinessAddress) && Objects.equals(businessExplain, that.businessExplain);
    }


    @Override
    public int hashCode() {
        return Objects.hash(businessId, password, businessName, buinessAddress, businessExplain, starPrices, deliveryPrice);
    }

    @Override
    public String toString() {
        return "BusinessMng{" +
                "businessId=" + businessId +
                ", password='" + password + '\'' +
                ", businessName='" + businessName + '\'' +
                ", buinessAddress='" + buinessAddress + '\'' +
                ", businessExplain='" + businessExplain + '\'' +
                ", starPrices=" + starPrices +
                ", deliveryPrice=" + deliveryPrice +
                '}';
    }
    public void ListToFile(List<BusinessMng> list) throws IOException {
        File file = new File("d:elm_v1/Business.txt");
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
        try {
            fileWriter = new FileWriter(file);
            bw = new BufferedWriter(fileWriter);
            for(int i = 0;i<list.size();i++){
                BusinessMng bus = list.get(i);
                StringBuilder sbr = new StringBuilder();
                sbr.append(bus.getBusinessId() + " ");
                sbr.append(bus.getPassword() + " ");
                sbr.append(bus.getBusinessName() + " ");
                sbr.append(bus.getBuinessAddress() + " ");
                sbr.append(getBusinessExplain() + " ");
                sbr.append(getStarPrices() + " ");
                sbr.append(getDeliveryPrice() + " ");

                bw.write(sbr.toString());
                bw.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            bw.close();
            fileWriter.close();
        }

    }
    public  List<BusinessMng> fileToList() throws IOException {
        List<BusinessMng> list = new ArrayList<>();
        File file = new File("d:/elm_v1/Business.txt");
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        while(true){
            String str = br.readLine();
            if(str != null){
                String[] arr = str.split(" ");
                BusinessMng business = new BusinessMng();
                business.setBusinessId(Integer.valueOf(arr[0]));
                business.setPassword(arr[1]);
                business.setBusinessName(arr[2]);
                business.setBuinessAddress(arr[3]);
                business.setBusinessExplain(arr[4]);
                business.setStarPrices(Double.valueOf(arr[5]));
                business.setDeliveryPrice(Double.valueOf(arr[6]));
                list.add(business);
            }else {
                break;
            }
        }
        return list;
    }
//    private int businessId;
//    private String password;
//    private String businessName;
//    private String buinessAddress;
//    private String businessExplain;
//    private double starPrices;
//    private double deliveryPrice;

    public static void main(String[] args) throws IOException {
        BusinessMng b = new BusinessMng();
        b.setBusinessId(32423);
        b.setPassword("fesfrsvrfs");
        b.setBusinessName("卤肉饭");
        b.setBuinessAddress("啥也没有");
        b.setBusinessExplain("debaidadwea");
        b.setStarPrices(10);
        b.setDeliveryPrice(20);
       // b.modifyBusiness();
        //b.deleteBusiness();
        List<BusinessMng> list = new ArrayList<>();
        list.add(b);
        b.ListToFile(list);
        b.deleteBusiness();
    }
}
